package jeons.admin.product.biz.impl;

import java.util.Iterator;

import java.util.List;

import jeons.admin.product.biz.GraphicBiz;
import jeons.admin.product.biz.ProductBiz;
import jeons.admin.product.biz.ProductImgBiz;
import jeons.admin.product.dto.GraphicDTO;
import jeons.admin.product.dto.ProductDTO;
import jeons.admin.product.dto.ProductImgDTO;
import jeons.common.util.FileService;
import jeons.common.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional
public class GraphicBizImpl extends SqlMapClientDaoSupport implements GraphicBiz{
		@Autowired SqlMapClientTemplate sqlMapClientTemplate;
		@Autowired FileService fileService;
		@Autowired ProductImgBiz productImgBiz;
		@Autowired ProductBiz productBiz;
		
		
		/**
		 * <PRE>
		 * 1. MethodName : insertData
		 * 2. ClassName  : GraphicBizImpl
		 * 3. Comment   :
		 * 4. �ۼ���    : Administrator
		 * 5. �ۼ���    : 2012. 1. 21. ���� 10:08:41
		 * </PRE>
		 *   @param graphicDTO
		 *   @param listimg
		 *   @param dteilimg
		 *   @return
		 */
		@Override
		public GraphicDTO insertData(GraphicDTO graphicDTO, List listimg, List dteilimg) {
			ProductDTO productDTO = new ProductDTO();
			productDTO.setPrice(graphicDTO.getPrice());
			productDTO.setType(graphicDTO.getType());
			productDTO.setP_number(graphicDTO.getP_number());
			productDTO.setProduct(graphicDTO.getProduct());
			productDTO.setRgdate(graphicDTO.getRgdate());
			productDTO.setManufacturer(graphicDTO.getManufacturer());
			
			sqlMapClientTemplate.insert("Product.insertData",productDTO);
			graphicDTO.setProno(productDTO.getProno());
			sqlMapClientTemplate.insert("Graphic.insertData",graphicDTO);
			
			String today = Utils.getYmdhhmmss("yyyyMMddhhmmss");
			ProductImgDTO productImgDTO = new ProductImgDTO();
			for (Iterator iterator = dteilimg.iterator();  iterator.hasNext();) {
				MultipartFile multipartFile = (MultipartFile)iterator.next();
				fileService.uploadData(multipartFile,"product",today);
				productImgDTO.setProno(productDTO.getProno());		// ��ǰ���̺� �ڵ�
				productImgDTO.setType(productDTO.getType());
				productImgDTO.setB_filename(today+multipartFile.getOriginalFilename());
				productImgDTO.setB_realname(multipartFile.getOriginalFilename());
				productImgDTO.setB_filesize((int)multipartFile.getSize());
				//System.out.println("listimg :"+multipartFile.getOriginalFilename());;
				
				
			}	//����
			for (Iterator iterator =  listimg.iterator();  iterator.hasNext();) {
				MultipartFile multipartFile = (MultipartFile)iterator.next();
				fileService.uploadData(multipartFile,"product",today);
				productImgDTO.setL_filename(today+multipartFile.getOriginalFilename());
				productImgDTO.setL_realname(multipartFile.getOriginalFilename());
				productImgDTO.setL_filesize((int)multipartFile.getSize());
				
			}
			productImgBiz.uploadImgData(productImgDTO);
			return graphicDTO;
		}


		@Override
		public GraphicDTO getData(int prono) {
			// TODO Auto-generated method stub
			return (GraphicDTO) sqlMapClientTemplate.queryForObject("Graphic.getData",prono);
		}


		@Override
		public void updateData(GraphicDTO graphicDTO) {
			ProductDTO productDTO = new ProductDTO();
			productDTO.setPrice(graphicDTO.getPrice());
			productDTO.setType(graphicDTO.getType());
			productDTO.setP_number(graphicDTO.getP_number());
			productDTO.setProduct(graphicDTO.getProduct());
			productDTO.setRgdate(graphicDTO.getRgdate());
			productDTO.setManufacturer(graphicDTO.getManufacturer());
			productDTO.setProno(graphicDTO.getProno());
			productBiz.updateData(productDTO);
			graphicDTO.setProno(productDTO.getProno());
			sqlMapClientTemplate.update("Graphic.updateData",graphicDTO);
			
		}


		@Override
		public GraphicDTO getALLData(int prono) {
			return (GraphicDTO) sqlMapClientTemplate.queryForObject("Graphic.getALLData",prono);
		}
		

}
